<?php
namespace app\mobile\controller;

use app\model\Util; 
use app\model\Theme; 
use app\model\Fans; 
use app\model\Share; 
use app\model\Scan; 

class Getconfig extends Base
{
    public function Getconfig()
    {p
global $_G, $_P;
$fans = $this->checkuser();

$system=$_G['system'];
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('活动入口不正确');
}
$theme = Theme::getThemeById($themeid);
if(empty($theme['id'])){
    die(Util::showError("请确认活动地址是否正确"));
    return;
}
$config = 	Theme::getConfig($themeid);
if (empty($system['endday'])) {
	$endday = 5;
}else{
	$endday = $system['endday'];
}
$endday = $endday*24*3600;
if ($fans['status']==2 || ($theme['endtime']+$endday) < TIMESTAMP) {
	$recordid = 0;
	if ($system['s']['ishongbao']==1) {
		$system['fenxiao'] = 0;
		$config['iscomm'] = 0;
	}
	if ($system['s']['isfenxiao']==1) {
		$system['fenxiao01'] = 0;
		$config['danmu02'] = 1;
		$config['danmu03'] = 1;
	}
	if ($system['s']['iszhuanfa']==1) {
		$system['fenxiao04'] = 0;
		$config['danmu03'] = 1;
		$config['danmu04'] = 1;
	}
	if ($system['s']['isyaoyiyao']==1) {
		$system['fenxiao07'] = 0;
		$config['danmu03'] = 1;
		$config['danmu04'] = 1;
	}
	if ($system['s']['isticheng']==1) {
		$system['fenxiao06'] = 0;
	}
	if ($system['s']['isyuedu']==1) {
		$system['fenxiao05'] = 0;
	}
	if ($system['s']['isicon']==1) {
		$config['isicon'] = 0;
		$config['danmu01'] = 1;
	}
	if ($system['s']['isfanpai']==1) {
		$system['fanpai'] = 0;
	}
	if ($system['s']['isfujintuan']==1) {
		$config['isrecent'] = 0;
		$config['marketprice'] = '0.00';
	}	
	if ($system['s']['isjpeg']==1) {
		$config['headlogo']=str_replace(".jpeg",".jpg",$config['headlogo']);
	}
	if ($system['s']['istime']==1) {
		$config['istime']=10;
	}
	if ($system['s']['isheadimg']==1) {
		$config['iswttouxiang']=10;
	}
	if ($system['s']['isdanmu']==1) {
		$config['isdanmu']=0;
	}
}

	if (($fans['status']==2 || ($theme['endtime']+$endday) < TIMESTAMP) && $system['s']['isjpeg']==1) {
		if (strripos($config['headlogo'], '.jpeg')) {
			$config['headlogo']=str_replace(".jpeg",".jpg",$config['headlogo']);
		}
		if (!empty($config['bannerimgs'])){
			foreach ($config['bannerimgs'] as $index => $row){
				if (strripos($row, '.jpeg')) {
					unset($config['bannerimgs'][$index]);
		        }
		    }
        }
		if (!empty($config['imgs'])){
			foreach ($config['imgs'] as $index => $row){
				if (strripos($row, '.jpeg')) {
					unset($config['imgs'][$index]);
		        }
		    }
        }
        if (!empty($config['centerimgs'])){
			foreach ($config['centerimgs'] as $index => $row){
				if (strripos($row, '.jpeg')) {
					unset($config['centerimgs'][$index]);
		        }
	        }
	    }
	    $cishu = 0 ;
	    while(strripos($config['content'], '.jpeg') && $cishu <10)
		{	
			$a = strripos($config['content'],'jpeg');
			$b = strripos(substr($config['content'],0,$a),'<img src=');
			$str = substr($config['content'],$b,$a-$b+50);
			$str = strstr($str,'/>',TRUE) ."/>";
			$config['content']=str_replace($str,"",$config['content']);
			$cishu = $cishu +1;
		}
		if (strripos($theme['addimg'], '.jpeg')) {
			$theme['addimg']="";
		}
	}


	if ((DOMAIN0 == "30.yixuan.net" || ($theme['endtime']+$endday) < TIMESTAMP || (!empty($theme['jiezhang']) && $theme['status']==2)) && $theme['yixuanbanquan']==0 && empty($theme['USERJISHU'])) {
		$isend = true;
		if($_G['companyname'] =='飞驰软件'){
			$imgright = "/92.jpg";
		}else{
			$imgright = "/91.jpg";
		}	
		$config['headlogo']=str_replace("/99.jpg",$imgright,$config['headlogo']);
		$config['content']=str_replace("/99.jpg",$imgright,$config['content']);
		
        if (!empty($config['bannerimgs'])){
            foreach ($config['bannerimgs'] as $index => $row){
                $config['bannerimgs'][$index]=str_replace("/99.jpg",$imgright,$row);
            }
        }
		if (!empty($config['imgs'])){
			foreach ($config['imgs'] as $index => $row){
				$config['imgs'][$index]=str_replace("/99.jpg",$imgright,$row);
	        }
        }
        if (!empty($config['centerimgs'])){
			foreach ($config['centerimgs'] as $index => $row){
				$config['centerimgs'][$index]=str_replace("/99.jpg",$imgright,$row);
	        }
	    }
	}

	$result['headlogo'] = Theme::encode(tomedia($config['headlogo']));

	if (!empty($theme['addimg'])) {
		$config['content'] = $config['content'] . "<img src='" . tomedia($theme['addimg']) . "' style='pointer-events:auto; width:100%;'  class='lazy'>";
	}
	$result['content'] = Theme::encode($config['content']);
		



	if (!empty($config['centerimgs'])){
		$centerimgs="";
		foreach ($config['centerimgs'] as $key => $item) {
			$centerimgs = $centerimgs . "<img class='lazy' style='width:100%' src='" . tomedia($item) . "' />";
		}
		$result['centerimgs'] = Theme::encode($centerimgs);
	}


	if (!empty($config['imgs'])){
	if ((!empty($config['imgs']) && $system['friend']==1) || ($config['isicon']==0 && $system['fenxiao07']==1)){
		$imgs="";
		foreach ($config['imgs'] as $key => $item) {
			$imgs = $imgs . "<img class='lazy' src='" . tomedia($item) . "' />";
		}
		$result['imgs'] = Theme::encode($imgs);
	}
	}
	// $result['desc'] = Theme::encode($theme['desc']);
	// if (empty($theme['img'])){
	// 	$shareimgurl = tomedia($config['headlogo']);
	// }else{
	// 	$shareimgurl = tomedia($theme['img']);
	// }
	// $result['imgUrl'] = Theme::encode($shareimgurl);




	//点亮星星
	if($config['isicon'] > 0){

        $lightsum = 0;
        if($config['iconnum2'] <= 0){
            $lightsum = Share::getNumByThemeIdAndOpenidAndStatus($themeid,$_G['openid'],1);
        }else{
        	if ($config['isicon']==2) {
            	$lightsum = Scan::getNumByThemeIdAndOpenid($themeid,$_G['openid'],1);;
        	}else{
            	$lightsum = Scan::getNumByThemeIdAndOpenidAndStatus($themeid,$_G['openid'],1);;
            }
            $lightsum = intval($lightsum/$config['iconnum2']);
        }

	    if ($config['isicon']==3) {
	    	$help_uid = $_P['helpuid'];
			if (!empty($help_uid)){
				$friendlistuid = $help_uid;
			}elseif (!empty($themyrecord) || $config['friendorder']==1){
				$friendlistuid = $_G['member']['uid'];
			}
			if (!empty($friendlistuid)){
				$friendfans = Fans::getById($friendlistuid,$themeid);
				$lightsum = $friendfans['friendnum'];
			}else{
				$lightsum = 0;
			}
			if (!empty($friendlistuid)){
				$friendfans = Fans::getById($friendlistuid,$themeid);
				$params = array();
			    
			    $params[':uid'] = $_G['member']['uid'];
			    $params[':fromuid'] = $help_uid;
			    $params[':themeid'] = $themeid;
			    $friend = db_find("SELECT * FROM ".tablename(t_friend)."
					WHERE uid = :uid and themeid = :themeid and fromuid = :fromuid",$params);

				if(empty($friend)){
					$isfriend = 0;
				}else{
					$isfriend = 1;
				}
			}
		}

        if ($config['iconlogo']!=1) {
			if($lightsum >= $config['iconnum']){
				$lightsum = $config['iconnum'];
			}
			 
			$lightlist = array();
			for($i=1;$i<=$lightsum;$i++){
				$lightlist[$i]=$i;
			}
			$darksum = $config['iconnum'] ;
			$darklist = array();

			for($i=$lightsum+1;$i<=$darksum;$i++){
				$darklist[$i]=$i;
			}
        }else{
			$shoplist = db_select ( "SELECT * FROM " . tablename (t_shangjia) .
					" WHERE  state = 1 and darkicon<>'' order by displayorder" );
			$iconnum = count($shoplist);

			if($lightsum >= $iconnum){
				$lightsum = $iconnum;
			}

			$lightlist = array();
			$darklist = array();
			foreach ($shoplist as $index => $item) {
				$i = $index+1;
				if ($i<=$lightsum) {
					$lightlist[$i] = str_replace("/99.jpg","/logo.jpg",$item['logo']);
				} else {
					$darklist[$i] = str_replace("/99.jpg","/logo.jpg",$item['darkicon']);
				}
			}
		}
		if ($config['isicon']==3) {
		    $iconlogo = " <div class='content star_wrap'>" ;
		    $iconlogo = $iconlogo . "   <ul class='content' style='background: ".$config['starbgcolor'].";'>";
		    if ($config['iconlogo']!=1){
		      foreach ($lightlist as $index => $item) {
		        $iconlogo = $iconlogo . "       <li><img src='" . tomedia($config['lighticon']) . "' class='lazy'></li>";
		      }
		      foreach ($darklist as $index => $item) {
		        $iconlogo = $iconlogo . "       <li><img onclick='dofriendhelp({$help_uid})' id='dark". $index ."' src='" . tomedia($config['darkicon']) . "' alt='' class='lazy'></li>";
		      }
		    }else{
		      foreach ($lightlist as $index => $item) {
		        $iconlogo = $iconlogo . "        <li><img src='" . tomedia($item) . "' alt='' class='lazy' ></li>";
		      }
		      foreach ($darklist as $index => $item) {
		        $iconlogo = $iconlogo . "        <li><img id='dark". $index ."' src='" . tomedia($item) . "' alt='' class='lazy' ></li>";
		      }
		    }
		    $iconlogo = $iconlogo . "   </ul>";
		    if (!empty($config['imgs']) && $system['friend']!=1){
		      $iconlogo = $iconlogo . "    <div class='content baixian'>";
		        foreach ($config['imgs'] as $key => $item) {
		          $iconlogo = $iconlogo . "        <img class='lazy' src='" . tomedia($item) . "' />";
		        }
		      $iconlogo = $iconlogo . "    </div>";
		    }
		    $iconlogo = $iconlogo . "   <ul class='content' style='padding-top: 10px; margin-top: 10px; padding-bottom: 8px; background: none;'>";
		    if (!empty($help_uid) && !empty($friendfans['username'])){
			    if ($isfriend == 0){
			    	$iconlogo = $iconlogo . "   <a class='new_light ' href='javascript:;' onclick='dofriendhelp({$help_uid})' style='background:".$config['starbtcolor'] . ";color:".$config['startextcolor']."'><span class='jlink'>" . $config['iconname'] . "</span></a>";
			    }else{
			    	$iconlogo = $iconlogo . "   <a class='new_light ' href='javascript:;' style='background:".$config['starbtcolor'] . ";color:".$config['startextcolor']."'>{$config['friend_success']}</a>";
			    }
			}else{
			    $iconlogo = $iconlogo . "   <a class='new_light ' href='javascript:;' style='background:".$config['starbtcolor'] . ";color:".$config['startextcolor']."'><span class='jlink'>" . $config['iconname'] . "</span></a>";
			}
		    $iconlogo = $iconlogo . "   </ul>";
		    $iconlogo = $iconlogo . " </div>";

		}else{
		    $iconlogo = " <div class='content star_wrap'>" ;
		    $iconlogo = $iconlogo . "   <ul class='content' style='background: ".$config['starbgcolor'].";'>";
		    if ($config['iconlogo']!=1){
		      foreach ($lightlist as $index => $item) {
		        $iconlogo = $iconlogo . "       <li><img src='" . tomedia($config['lighticon']) . "' class='lazy'></li>";
		      }
		      foreach ($darklist as $index => $item) {
		        $iconlogo = $iconlogo . "       <li><a href='javascript:letgo();'><img id='dark". $index ."' src='" . tomedia($config['darkicon']) . "' alt='' class='lazy'></a></li>";
		      }
		    }else{
		      foreach ($lightlist as $index => $item) {
		        $iconlogo = $iconlogo . "        <li><img src='" . tomedia($item) . "' alt='' class='lazy' ></li>";
		      }
		      foreach ($darklist as $index => $item) {
		        $iconlogo = $iconlogo . "        <li><img id='dark". $index ."' src='" . tomedia($item) . "' alt='' class='lazy' ></li>";
		      }
		    }
		    $iconlogo = $iconlogo . "   </ul>";
		    if (!empty($config['imgs']) && $system['friend']!=1){
		      $iconlogo = $iconlogo . "    <div class='content baixian'>";
		        foreach ($config['imgs'] as $key => $item) {
		          $iconlogo = $iconlogo . "        <img class='lazy' src='" . tomedia($item) . "' />";
		        }
		      $iconlogo = $iconlogo . "    </div>";
		    }
		    $iconlogo = $iconlogo . "   <ul class='content' style='padding-top: 10px; margin-top: 10px; padding-bottom: 8px; background: none;'>";
		    $iconlogo = $iconlogo . "   <a class='new_light ' href='javascript:letgo();' style='background:".$config['starbtcolor'] . ";color:".$config['startextcolor']."'><span class='jlink'>" . $config['iconname'] . "</span></a>";
		    $iconlogo = $iconlogo . "   </ul>";
		    $iconlogo = $iconlogo . " </div>";
		}
		$result['iconlogo'] = Theme::encode($iconlogo);
	} 





$result['errcode'] = 0;
return_msg($result, '', 'ajax');
}
}